﻿using BrnMall.Core;
using BrnMall.Manager;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.Order
{
    public partial class PrintConfigs
    {
        public static List<PrintConfig> GetPrintList(int StoreID) {
            using (var db = new QDMallDatabase()) {
                return db.Fetch<PrintConfig>("select * from bma_PrintConfig where StoreID=@0", StoreID);
            }
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="ShopID"></param>
        /// <returns></returns>
        public static List<PrintConfig> GetPrintByShopID(int StoreID)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<PrintConfig>("select PrintID,PrintFormat from bma_PrintConfig where StoreID=@0", StoreID);
            }
        }

        public static bool IsExistsPrintConfig(PrintConfig pc) {
            using (var db = new QDMallDatabase()) {
                return db.ExecuteScalar<int>("select count(1) from bma_PrintConfig where StoreID=@0 and PrintID=@1 and PrintFormat=@2",
                    pc.StoreID,pc.PrintID,pc.PrintFormat) > 0;
            }
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="ShopID"></param>
        /// <param name="PrintID"></param>
        /// <returns></returns>
        public static int InsertPrintConfig(PrintConfig pc)
        {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.Execute("insert into bma_PrintConfig values(@0,@1,@2)", pc.StoreID, pc.PrintID,pc.PrintFormat);
            }
            return obj;
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="ShopID"></param>
        /// <param name="PrintID"></param>
        /// <returns></returns>
        public static int UpdatePrintConfig(PrintConfig pc)
        {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.Execute("update bma_PrintConfig set PrintID=@0,PrintFormat=@1 where StoreID=@2", pc.PrintID,pc.PrintFormat,pc.StoreID);
            }
            return obj;
        }

        /// <summary>
        /// 删除该店铺数据
        /// </summary>
        /// <param name="ShopID"></param>
        /// <returns></returns>
        public static int DeletePrintConfig(PrintConfig pc)
        {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.Execute("delete from bma_PrintConfig where StoreID=@0 and PrintID=@1 and PrintFormat=@2", pc.StoreID,pc.PrintID,pc.PrintFormat);
            }
            return obj;
        }

        public static string GetPrintConfigIP(int StoreID) {
            string IP = "";
            using (var db = new QDMallDatabase())
            {
                int count = db.ExecuteScalar<int>($"select count(1) from bma_printconfigip where StoreID=@0",StoreID);
                if (count <= 0)
                    db.Execute("insert into bma_printconfigip values(@0,'localhost:8000')",StoreID);
                IP = db.ExecuteScalar<string>("select JSIP from bma_printconfigip where StoreID=@0",StoreID);
            }
            return IP;
        }

        public static int UpdatePrintConfigIP(int StoreID,string ip) {
            int obj = 0;
            using (var db = new QDMallDatabase())
            {
                obj = db.Execute("update bma_printconfigip set JSIP=@0 where StoreID=@1", ip,StoreID);
            }
            return obj;
        }
    }
}
